<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/21
 * Time: 11:47
 */

namespace Home\Controller;


use Common\Logic\VideoApiLogic;

class PlayRealtimeController extends HomeController
{

    public function index(){
        $this->meta_title='实时回传视频';
        $this->display();
    }

    public function index2(){
        $this->meta_title = 'test';
        $this->display();
    }

    public function realtimeList($limit, $offset,$searchOption, $search = null){
        $fields = "vrt.*, o.org_name";
        if(!empty($this->uids)){
            $map['_string'] = "vrt.user_id in(".implode(',', $this->uids).")";
        }
        $map['vrt.state'] = 1;
        $map['vrt.is_center'] = 0;
        if(!empty($search)){
            if($searchOption == 1){
                $map['o.org_name'] = array('like', "%{$search}%");
            }elseif ($searchOption == 2){
                $map['u,user_name'] = array('like', "%{$search}%");
            }
        }
        $order = 'add_time desc';
        $record = M('VideoRealTime')->alias('vrt')->join('user u on vrt.user_id=u.user_id')->join('organizations o on u.org_id=o.org_id')->field($fields)->where($map)->order($order)->limit($limit, $offset)->select();
        $total_count = M('VideoRealTime')->alias('vrt')->join('user u on vrt.user_id=u.user_id')->join('organizations o on u.org_id=o.org_id')->where($map)->count();
        $result['rows'] = $record;
        $result['total'] = $total_count;
        $this->ajaxReturn($result);
    }

    public function delete(){
        $ids = I('selectedIds');
        $where['real_time_id'] = array('in', $ids);
        $res = M('VideoRealTime')->where($where)->setField('state', 0);
        if($res !== false){
            logOperation('实时回传视频','删除实时视频');
            $this->sucessJson();
        }else{
            $this->errorJson('删除失败');
        }
    }
    public function changeOpen($id, $val){

        $where['real_time_id'] = $id;
        $res = M('VideoRealTime')->where($where)->setField('is_open', $val);
        if($res !== false){
            logOperation('实时视频','修改视频开放权限');
            $this->sucessJson();
        }else{
            $this->errorJson('修改失败');
        }
    }
    public function createNew(){
        $res = VideoApiLogic::createLiveTelecast(UID);
        $this->ajaxReturn($res);
    }

    public function connect(){
//        $res = array(
//            'data'=>array(
//                array(
//                    'live_name'=>'21_1_20170106_1483688472',
////                    'videoURL'=>'rtmp://pili-publish.moboa.adreep.cn/bdsoft-live/21_1_20170106_1483688472?e=1483692072&token=l_3M6rQ53sICrLok_OSFi7hkTq9dVN61CLIxd_NG:vaa2LMnTIyufgIU2H9uRv3oBDXE='
//                    'videoURL'=>'rtmp://pili-publish.moboa.adreep.cn/bdsoft-live/21_11_20170113_1484293149?e=1484296749&token=l_3M6rQ53sICrLok_OSFi7hkTq9dVN61CLIxd_NG:JI08S0H_PmFZiIwNdA-qdCRAaQM='
//                )
//            ),
//            'state' => '1',
//            'message' => '创建成功'
//        );

        $res = VideoApiLogic::createLiveTelecast(UID);
//        die(json_encode($res));
        $pullUrl = $res['data'][0]['videoURL'];
        if($res['state'] == 1){
//            $res['pushUrl'] = clear_url_param($res['data'][0]['videoURL']);
            $res['pushUrl'] = $res['data'][0]['videoURL'];
        }
        $where['user_id'] = UID;
        $where['state'] = 1;
        $where['is_center'] = 1;
        if(!empty($this->uids)){
            $where['_string'] = "user_id in(".implode(',', $this->uids).")";
        }

        $rt = M('VideoRealTime')->where($where)->order('real_time_id desc')->find();
        if($rt){
            $res['pullUrl'] = $rt['video_url'];
        }else{
            $res['pullUrl'] = $pullUrl;
        }
//        $d = $res['data'];
//        $d = $d[0];
//        $v = $d['videoURL'];
        $_videoUrl = $res['data'][0]['videoURL'];
        $pos = strpos($_videoUrl, 'live_cctv');
        if($pos !== false){
            $res['videoUrl'] = substr($_videoUrl, 0, $pos + 10);
            $res['streamName'] = substr($_videoUrl,$pos + 10);
        }else{
            $pos = strpos($_videoUrl, 'bdsoft-live');
            if ($pos!== false){
                $res['videoUrl'] = $res['pullUrl'];
                $res['streamName'] = substr($_videoUrl,$pos + 12);
            }
        }

//        $res['pullUrl'] = "rtmp://pili-publish.moboa.adreep.cn/bdsoft-live/21_11_20170113_1484293149";
        $this->ajaxReturn($res);
    }

    public function close($videoName){
        $res = VideoApiLogic::closeLiveTelecast($videoName);
        $this->ajaxReturn($res);
    }

    public function testPush(){
        $this->display();
    }

//    public function testUrl(){
//        $url = 'rtmp://pili-publish.moboa.adreep.cn/bdsoft-live/21_1_20170106_1483688472?e=1483692072&token=l_3M6rQ53sICrLok_OSFi7hkTq9dVN61CLIxd_NG:vaa2LMnTIyufgIU2H9uRv3oBDXE=';
//        $rs = clear_url_param($url);
//        $this->ajaxReturn($rs);
//    }

    public function testQiniu(){
        $this->display();
    }

}